Skip to content

Conversation

@k4anubhav
Copy link

@k4anubhav k4anubhav commented Oct 13, 2022

add select_polymorphic_related, prefetch_polymorphic_related and custom_queryset methods for select and prefetch on inherited models

Triggers Django errors when that field doesn't exist in a particular subclass

Example works exactly same as django select_related and prefetch_related

queryset = Project.objects.select_polymorphic_related(
    ArtProject, 'artist', 'canvas__painter'
).select_polymorphic_related(
    ResearchProject, 'supervisor',
)


queryset = Project.objects.prefetch_polymorphic_related(
    ArtProject, 'artist', Prefetch('canvas', queryset=Project.objects.annotate(size=F('width') * F('height')))
).prefetch_polymorphic_related(
    ResearchProject, 'authors',
)


queryset = Project.objects.custom_queryset(ArtProject, ArtProject._base_objects.annotate(cost=F('cost') + F('canvas_cost'))))

@k4anubhav
Copy link
Author

#198

@k4anubhav k4anubhav changed the title Support select_related and prefetch_related for inherited models Support select_related and prefetch_related for inherited models and custom queryset for child models Nov 8, 2022
@0legRadchenko
Copy link

Hi! Thank you for your commits! =)
Right now there are some problems If Im trying to combine select_related and select_polymorphic_related in one query (in such case It dupicates queries). So its not possible to use them simultaneously without making extra duplicated queries.

But if I use select_polymorphic_related without select_related everything working fine and vice versa If I use select_related without select_polymorphic_related in one query everything working good as well.

I've sent you additional info in telegram :)

@0legRadchenko
Copy link

Now all is great 🙌🏻 Ive tested select_related and select_prefetch_related together and also separatly and did not manage to find any duplicates in queries and so far so good.

@Safrone
Copy link

Safrone commented Dec 6, 2022

Anything holding this back? Feels like it would be a big win if it "just works"

@alphatownsman
Copy link

it would be really really nice to have this

@mazulo
Copy link

mazulo commented Feb 24, 2023

Hey team, when can we expect to get this merged?

@FrancoisDupayrat
Copy link

Thanks a lot for this PR. It single-handedly solved the biggest performance issue in my production app.
I can confirm it works. Hopefully it will get merged.

pgammans added a commit to bva-icx/django-polymorphic that referenced this pull request Jun 26, 2023
implement support for a single query for select related base fetches across
polymorphic models.

adds a polymorphic QuerySet Mixin to enable non polymorphic models to fetch
related models.

Fixes: jazzband#198 jazzband#436 jazzband#359 jazzband#244
Possible Fixes:
  jazzband#498: support for prefetch_related cannot fetch attributes not on all child models or via class names
Related: jazzband#531
pgammans added a commit to bva-icx/django-polymorphic that referenced this pull request Jun 26, 2023
implement support for a single query for select related base fetches across polymorphic models.

adds a polymorphic QuerySet Mixin to enable non polymorphic models to fetch related models.

fixes: jazzband#198 jazzband#436 jazzband#359 jazzband#244
possible fixes:
    jazzband#498: support for prefetch_related cannot fetch attributes not on all child models or via class names
related: jazzband#531
@Safrone
Copy link

Safrone commented Sep 8, 2023

I created a Gist showing how to use this enhancement without having to pin this PR version or modify Django Polymorphic:
https://gist.github.com/Safrone/7c24b492a791679e6e94cc765459dc78

I haven't gotten to do extensive testing on it so anyone using it finding issues please let me know

andriilahuta pushed a commit to andriilahuta/django-polymorphic that referenced this pull request Dec 19, 2023
implement support for a single query for select related base fetches across polymorphic models.

adds a polymorphic QuerySet Mixin to enable non polymorphic models to fetch related models.

fixes: jazzband#198 jazzband#436 jazzband#359 jazzband#244
possible fixes:
    jazzband#498: support for prefetch_related cannot fetch attributes not on all child models or via class names
related: jazzband#531
pgammans added a commit to bva-icx/django-polymorphic that referenced this pull request Apr 4, 2024
implement support for a single query for select related base fetches across polymorphic models.

adds a polymorphic QuerySet Mixin to enable non polymorphic models to fetch related models.

fixes: jazzband#198 jazzband#436 jazzband#359 jazzband#244
possible fixes:
    jazzband#498: support for prefetch_related cannot fetch attributes not on all child models or via class names
related: jazzband#531
pgammans added a commit to bva-icx/django-polymorphic that referenced this pull request Apr 4, 2024
implement support for a single query for select related base fetches across polymorphic models.

adds a polymorphic QuerySet Mixin to enable non polymorphic models to fetch related models.

fixes: jazzband#198 jazzband#436 jazzband#359 jazzband#244
possible fixes:
    jazzband#498: support for prefetch_related cannot fetch attributes not on all child models or via class names
related: jazzband#531
ktowen pushed a commit to acuangel/django-polymorphic that referenced this pull request Jan 18, 2025
implement support for a single query for select related base fetches across polymorphic models.

adds a polymorphic QuerySet Mixin to enable non polymorphic models to fetch related models.

fixes: jazzband#198 jazzband#436 jazzband#359 jazzband#244
possible fixes:
    jazzband#498: support for prefetch_related cannot fetch attributes not on all child models or via class names
related: jazzband#531
@agalazis
Copy link

agalazis commented Nov 27, 2025

@0legRadchenko any news on this feature? it's a must
I also suggest that we do:

            real_objects = real_concrete_class._base_objects.db_manager(self.db)
            if self._polymorphic_custom_queryset.get(real_concrete_class):
                real_objects = self._polymorphic_custom_queryset[real_concrete_class](real_objects)

for custom_queryset option so that we can customise the original queryset in a lambda. If we want something entirely custom then the lambda can return whatever custom without relying on the original one

@bckohan bckohan force-pushed the master branch 4 times, most recently from b94f2be to 27a4fe4 Compare December 8, 2025 18:05
@codecov
Copy link

codecov bot commented Dec 8, 2025

Codecov Report

❌ Patch coverage is 39.28571% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.81%. Comparing base (6e2c702) to head (e1b41bb).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/polymorphic/query.py 39.28% 14 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #531      +/-   ##
==========================================
- Coverage   75.57%   74.81%   -0.76%     
==========================================
  Files          21       21              
  Lines        1347     1374      +27     
  Branches      213      217       +4     
==========================================
+ Hits         1018     1028      +10     
- Misses        257      271      +14     
- Partials       72       75       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bckohan
Copy link
Contributor

bckohan commented Dec 8, 2025

Need to compare this solution to #545

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants